package net.kts.story;

import java.util.List;

import net.kts.help.HelpLayout;
import net.kts.help.HelpService;
import net.kts.main.R;
import net.kts.story.help.StoryHelpLayout;
import net.kts.story.map.StoryMap;
import net.kts.story.map.overlay.StoryItemizedOverlayExtends;
import net.kts.story.menu.StoryMenu;
import net.kts.story.twitter.contator.TwitterConator;
import net.kts.transrate.TranslateService;
import twitter4j.GeoLocation;
import twitter4j.Status;
import twitter4j.Tweet;
import twitter4j.TwitterException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Handler;
import android.text.NoCopySpan.Concrete;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class StoryObjectHouse {
	private final MapView mapView; 
	private final Activity mainActivity;
	private static StoryMenu storyMenu;
	private final StoryMap storyMap;
	private Location currentLocation;
	private GeoPoint selectedLocation;
	private final TwitterConator twiitterConator;
	
	public StoryObjectHouse(Story story, MapView mapView, TwitterConator twitterConator) {
		this.mapView = mapView;
		this.mainActivity = story;
		storyMenu = new StoryMenu(this);
		this.twiitterConator = twitterConator;
		this.storyMap = new StoryMap(this, mapView);
		
		new Handler().post(new SettingHandler());
	}
	
	public void updateWidthNewLocation() {
		Log.d("kts", "updateWidthNewLocation");
		Location location = storyMap.refreshGeoLocation();
		
		Double geoLat = location.getLatitude() * 1000000;
		Double geoLng = location.getLongitude() * 1000000;
		
		GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
		mapView.getController().animateTo(point);
		
		setCurrentLocation(location);
		
		
		surroundTwitter();
	}
	
	public void surroundTwitterAndMyTweet(GeoLocation location){
		if(!twiitterConator.isAuthMemberTwitter())
			return;
		
		Drawable drawable = mainActivity.getResources().getDrawable(R.drawable.story_flag_man);
		StoryItemizedOverlayExtends itemizedOverlay = new StoryItemizedOverlayExtends(
				drawable,
				this.mapView,
				this.storyMenu);
		
		settingLocationTweetList(itemizedOverlay);

		try {
			Status homeLine = twiitterConator.getHomeLine();
			itemizedOverlay.addTweetOverlay(homeLine, location);
			
			boolean geoEnabled = homeLine.getUser().isGeoEnabled();
			if(!geoEnabled){
				AlertDialog.Builder alt_bld = new AlertDialog.Builder(mainActivity);
		    	alt_bld.setMessage(TranslateService.translate("[KoreaTravelStory]는 [Twitter의 위치정보 추가] 기능 설정이 되어 있어야 합니다."));
		    	alt_bld.setPositiveButton( "close", new DialogInterface.OnClickListener() {
		    	    public void onClick( DialogInterface dialog, int which) {
		    	        dialog.dismiss(); 
		    	        new HelpService(mainActivity)
		    	        	.add(new StoryHelpLayout(R.drawable.story_twitter_help));
		    	    }
		    	});
		    	alt_bld.setIcon(R.drawable.story_twitter_help);
		    	AlertDialog alert = alt_bld.create();
		    	alert.show();
			}
		} catch (TwitterException e) {
			Log.e("kts", e.getMessage());
		}
		
		
		
			
		Log.d("kts", "twitter ing after");
		Log.d("kts", "size " + itemizedOverlay.size());
		if (itemizedOverlay.size() > 0) 
			this.mapView.getOverlays().add(1, itemizedOverlay);
	}
	public void surroundTwitter() {
		Drawable drawable = mainActivity.getResources().getDrawable(R.drawable.story_flag_man);
		StoryItemizedOverlayExtends itemizedOverlay = new StoryItemizedOverlayExtends(
				drawable,
				this.mapView,
				this.storyMenu);
		
		
		
		settingLocationTweetList(itemizedOverlay);
		
		if(twiitterConator.isAuthMemberTwitter()){
			try {
				Status homeLine = twiitterConator.getHomeLine();
				Log.e("kts",(homeLine == null)+"");
				Log.e("kts",homeLine.getUser().getName());
				Log.e("kts",homeLine.getText());
				Log.e("kts",homeLine.getUser().getProfileImageURL() +"");
			} catch (TwitterException e) {
				Log.e("kts", e.getMessage());
			}
		
		}
			
		Log.d("kts", "twitter ing after");
		Log.d("kts", "size " + itemizedOverlay.size());
		if (itemizedOverlay.size() > 0) 
			this.mapView.getOverlays().add(1, itemizedOverlay);
	}

	private void settingLocationTweetList(
			StoryItemizedOverlayExtends itemizedOverlay) {
		Location currentLocation = this.currentLocation;
		List<Tweet> listTweet = null;
		try {
			listTweet = twiitterConator.listFindByGeoLocation(
					currentLocation.getLatitude(), 
					currentLocation.getLongitude());
		} catch (TwitterException e) {
			Log.d("kts", e.getMessage());
		}
		
		Log.d("kts", "list tweet size : " + listTweet.size());
		for (Tweet tweet : listTweet) {
			GeoLocation location = tweet.getGeoLocation();
			if (location != null){
			    String text = tweet.getText().trim();
				if( !text.equals("#KoreaTravel")  &&  !text.equals("") )
					itemizedOverlay.addTweetOverlay(tweet, location);
			}
		}
	}
	
	public Location getCurrentLocation(){
		return currentLocation;
	}
	
	public void setCurrentLocation(Location location){
		this.currentLocation = location;
	}
	
	public GeoLocation getSelectedLocation() {
		double geoLat;
		double geoLng;
		if(selectedLocation != null){
			geoLat = selectedLocation.getLatitudeE6()/1000000.0;
			geoLng = selectedLocation.getLongitudeE6()/1000000.0;
		}else{
			geoLat = currentLocation.getLatitude();
			geoLng = currentLocation.getLongitude();	
		}
		GeoLocation geoLocation = new GeoLocation(
				geoLat, 
				geoLng);
		
		return geoLocation;
	}

	public void setSelectedLocation(GeoPoint selectedLocation) {
		this.selectedLocation = selectedLocation;
	}

	final class SettingHandler implements Runnable{
		@Override
		public void run() {
		 	storyMenu.addMenuContentView();
	        storyMenu.addMemoriesMenuContentView();
	        storyMenu.addMessageContentView();
		}
	}
	public void showMessage(String message){
		Toast.makeText(mainActivity, TranslateService.translateRepository(message), 1).show();
	}
	public StoryMap getStoryMap() {
		return storyMap;
	}

	public Activity getMainActivity() {
		return this.mainActivity;
	}
	public TwitterConator getTwitterConator() {
		return this.twiitterConator;
	}
	public static StoryMenu getStoryMenu() {
		return storyMenu;
	}
}
